#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

void retirementIncome()
{
    double sB; //starting balance
	double aC; //annual contributions
	int cA; //current age
	int aoR; //age of retirement
	double rrbR; //rate of return before retirement
	double rrdR; //rate of return during retirement
	double ctR; //current tax rate
	double rtR; //retirement tax rate
	double iR; //inflation rate
	int yoR; //years of retirement
	double eB = 0; //ending balance
	int yuR = 0; //years until retirement = (aoR - cA)
	double mI = 0; //monthly income, post-tax & inflation

	cout << "Welcome to Wang's Retirement Income Calculator" << endl;
	cout << endl;
	cout << "The algorithm/formula employed assumes:\n";
	cout << "-Steady, annual contributions until retirement" << endl;
	cout << "-Compounded annual returns with no withdrawals prior to retirement" << endl;
	cout << "-A monthly income retirement amount with no further contributions\n";
	cout << " during retirement" << endl;
	cout << "-That annual contributions occur at the start of each year" << endl;
	cout << endl;

	cout << "Enter the starting balance of your retirement account: $";
	cin >> sB;
	cout << endl;
	cout << "Enter the annual contributions you make to your retirement account: $";
	cin >> aC;
	cout << endl;
	cout << "Enter your current age (in years): ";
	cin >> cA;
	cout << endl;
	cout << "Enter your anticipated age at retirement (in years): ";
	cin >> aoR;
	cout << endl;
	cout << "Enter the rate of return of your equity before retirement: ";
	cin >> rrbR;
	cout << endl;
	cout << "Enter the anticipated/expected rate of return during retirement: ";
	cin >> rrdR;
	cout << endl;
	cout << "Enter your current tax rate (in decimal form): ";
	cin >> ctR;
	cout << endl;
	cout << "Enter your anticipated/expected tax rate during retirement: ";
	cin >> rtR;
	cout << endl;
	cout << "Enter the number of years of retirement your money will need to last: ";
	cin >> yoR;
	cout << endl;
	cout << "Enter the inflation rate: ";
	cin >> iR;
	cout << endl;

	//compounding interest calculation below here

	double R = 0; //actual rate during accumulation

	R = rrbR - iR; //actual rate = rate of return before retirement - inflation rate

	yuR = aoR - cA; //years until retirement = age of retirement - current age

	int yuR1 = 0;

	yuR1 = yuR + 1;

	eB = (sB * pow(1 + R, yuR)) + (aC * ((pow(1 + R, yuR1) - (1 + R)) / R));

	cout << "Your total ending balance right up to retirement is: $" << fixed << setprecision(2) << eB << endl;
	cout << "*Note that this amount has been accumulating pre-tax. You will\n";
	cout << "owe income tax once you begin making withdrawals from this account." << endl;
	cout << endl;

	//annuity calculation below here

	double awA = 0; //annual withdrawal amount (pretax)
	double R1 = 0; //actual rate during withdrawal
	R1 = rrdR - iR; //rate of return during retirement - inflation rate
	int yoR1 = 0;
	yoR1 = yoR + 1;

	awA = (eB * pow(1 + R1, yoR1) * R1) / (pow(1 + R1, yoR) - 1);

	mI = (awA * (1 - rtR)) / 12;

	cout << "Your monthly income at age " << aoR << " for " << yoR << " years after\n";
	cout << "taxes and inflation have been accounted for will be: $" << fixed << setprecision(2) << mI;
	cout << endl;

    system("PAUSE");

}